import { wsPrefix } from '@/consts/consts'

export default ({ $mqttService:ws }) => ({

  launcherFeedback(fb) {
    ws.on('connect', (e) => {
      console.log('连接成功: xr/launcher/feedback')
      ws.subscribe('xr/launcher/feedback', { qos: 1 }, (error) => {
        if (!error) {
          console.log('订阅成功: xr/launcher/feedback')
        } else {
          console.log('订阅失败: xr/launcher/feedback')
        }
      })
    })
    // 接收消息处理
    ws.on('message', (topic, message) => {
      console.log('收到来自xr/launcher/feedback:', topic, '的消息', message.toString())
      let obj = JSON.parse(message.toString())
      fb(obj)
    })
    // 断开发起重连
    ws.on('reconnect', (error) => {
      console.log('正在重连xr/launcher/feedback: ', error)
    })
    // 链接异常处理
    ws.on('error', (error) => {
      console.log('连接失败xr/launcher/feedback:', error)
    })

  }
})
